package com.itheima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Book;
import com.itheima.pojo.MyPage;
import com.itheima.pojo.MyQw;
import com.itheima.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * 增加一本书        /books          post        参数为RequestBody提供
 * 删除一本书        /books/id       delete      参数由路径提供
 * 修改一本书        /books          put         参数由RequestBody提供
 * 查询一本书        /books/id       get         参数由路径提供
 * 查询全部         /books           get        无参数
 *
 */

@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private IBookService iBookService;

    //增加
    @PostMapping
    public boolean save(@RequestBody Book book){
        return iBookService.save(book);
    }

    //删除
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable("id") Integer id){
        return iBookService.removeById(id);
    }

    //修改
    @PutMapping
    public boolean update(@RequestBody Book book){
        return iBookService.updateById(book);
    }

    //查询全部
    @GetMapping
    public List<Book> getAll(){
        return iBookService.list();
    }


    //分页及多条件查询
    @GetMapping("/{current}/{size}")
    public IPage<Book> getInit(@PathVariable("current") long current, @PathVariable("size") long size, MyQw myQw){
        IPage<Book> page = iBookService.initListPage(current, size, myQw.getTypeinput(), myQw.getNameinput());
        if(current > page.getPages()){
            current = 1;
            page = iBookService.initListPage(current, size, myQw.getTypeinput(), myQw.getNameinput());
        }
        return page;
    }


}
